﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RestSharp;
using RestSharp.Authenticators;
using Xunit;

namespace WebTest
{
  public partial class Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      string ConsumerKey = "6b80a3bd477b994f965f";
      string ConsumerSecret = "40fbad2ba5f46f1521fce2731a52a5fd13ad584c";
      var client = new RestClient("https://api.xing.com")
      {
        Authenticator = OAuth1Authenticator.ForRequestToken(ConsumerKey, ConsumerSecret, "http://localhost:62285/CallBack.aspx")
      };
      var request = new RestRequest("v1/request_token", Method.POST);
      var response = client.Execute(request);

      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.Created, response.StatusCode);

      var qs = HttpUtility.ParseQueryString(response.Content);
      var oauth_token = qs["oauth_token"];
      var oauth_token_secret = qs["oauth_token_secret"];
      Assert.NotNull(oauth_token);
      Assert.NotNull(oauth_token_secret);

      request = new RestRequest("/v1/authorize");
      request.AddParameter("oauth_token", oauth_token);
      var url = client.BuildUri(request).ToString();
      Response.Redirect(url);

      var verifier = "6594"; 
      request = new RestRequest("/v1/access_token", Method.POST);
      client.Authenticator = OAuth1Authenticator.ForAccessToken(
      ConsumerKey, ConsumerSecret, oauth_token, oauth_token_secret, verifier
      );
      response = client.Execute(request);

      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);

      qs = HttpUtility.ParseQueryString(response.Content);
      oauth_token = qs["oauth_token"];
      oauth_token_secret = qs["oauth_token_secret"];
      Assert.NotNull(oauth_token);
      Assert.NotNull(oauth_token_secret);

      request = new RestRequest("account/verify_credentials.xml");
      client.Authenticator = OAuth1Authenticator.ForProtectedResource(
      ConsumerKey, ConsumerSecret, oauth_token, oauth_token_secret
      );

      response = client.Execute(request);

      Assert.NotNull(response);
      Assert.Equal(HttpStatusCode.OK, response.StatusCode);


    }
  }
}